package com.zs.propertymanage.controller;

import com.zs.propertymanage.annotation.SystemLog;
import com.zs.propertymanage.dataObj.R;
import com.zs.propertymanage.dataObj.entity.Employee;
import com.zs.propertymanage.service.impl.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/employee")
@CrossOrigin
public class EmployeeController {

    // 构造器注入
/*    private final EmployeeService employeeService;

    @Autowired
    public EmployeeController(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }*/

    // Field注入
    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有的employee 原始数据
     *
     * @return R
     */
    @GetMapping("/plain")
    @ResponseBody
    public R getAllEmployee() {
        return employeeService.getAllEmployee();
    }

    /**
     * 查询所有的员工 包括附加信息
     *
     * @return R
     */
    @GetMapping("")
    @ResponseBody
    public R getAllEmployeeWithExtra(String search, Integer currentPage, Integer pageSize) {
        return employeeService.getAllEmployeeWithExtra(search, currentPage, pageSize);
    }

    /**
     * 查询所有的员工组织
     *
     * @return R
     */
    @GetMapping("/org")
    @ResponseBody
    public R getAllEmployeeOrg() {
        return employeeService.getAllEmployeeOrg();
    }

    /**
     * 查询所有的员工角色
     *
     * @return R
     */
    @GetMapping("/role")
    @ResponseBody
    public R getAllEmployeeRole() {
        return employeeService.getAllEmployeeRole();
    }

    /**
     * 添加员工
     *
     * @param employee 员工
     * @return R
     */
    @PostMapping("")
    @ResponseBody
    @SystemLog(name = "添加员工")
    public R addEmployee(@RequestBody Employee employee) {
        return employeeService.addEmployee(employee);
    }

    /**
     * 更新员工
     *
     * @param employee 员工
     * @return R
     */
    @PutMapping("")
    @ResponseBody
    @SystemLog(name = "更新员工")
    public R updateEmployee(@RequestBody Employee employee) {
        return employeeService.updateEmployee(employee);
    }

    /**
     * 删除员工
     *
     * @param id 员工id
     * @return R
     */
    @DeleteMapping("/{id}")
    @ResponseBody
    @SystemLog(name = "删除员工")
    public R deleteEmployee(@PathVariable String id) {
        return employeeService.deleteEmployee(id);
    }

    /**
     * 删除员工角色
     *
     * @param id 员工角色id
     * @return R
     */
    @DeleteMapping("role/{id}")
    @ResponseBody
    @SystemLog(name = "删除员工角色")
    public R deleteEmployeeRoleById(@PathVariable Integer id) {
        return employeeService.deleteEmployeeRoleById(id);
    }

    /**
     * 添加员工角色
     *
     * @param roleName 员工角色名称
     * @return R
     */
    @PostMapping("/role")
    @ResponseBody
    @SystemLog(name = "添加员工角色")
    public R addEmployeeRole(String roleName) {
        return employeeService.addEmployeeRole(roleName);
    }

    // 删除员工组织
    @DeleteMapping("org/{id}")
    @ResponseBody
    @SystemLog(name = "删除员工组织")
    public R deleteEmployeeOrgById(@PathVariable Integer id) {
        return employeeService.deleteEmployeeOrgById(id);
    }

    // 添加员工组织
    @PostMapping("/org")
    @ResponseBody
    @SystemLog(name = "添加员工组织")
    public R addEmployeeOrg(String orgName) {
        return employeeService.addEmployeeOrg(orgName);
    }

    // 查询未被接收的工单（status==1）
    @GetMapping("/unreceived/{employeeId}")
    @ResponseBody
    public R getUnreceivedWorkOrder(@PathVariable String employeeId) {
        return employeeService.getUnreceivedWorkOrder(employeeId);
    }

    // 员工接收工单
    @PutMapping("/receive")
    @ResponseBody
    @SystemLog(name = "接收工单")
    public R receiveWorkOrder(@RequestBody Map<String, String> map) {
        String employeeId = map.get("employeeId");
        String workOrderId = map.get("workOrderId");
        return employeeService.receiveWorkOrder(employeeId, workOrderId);
    }

    // 查询员工接收的工单
    @GetMapping("/received/{employeeId}")
    @ResponseBody
    public R getReceivedWorkOrder(@PathVariable String employeeId, String search, Integer currentPage, Integer pageSize) {
        return employeeService.getReceivedWorkOrder(employeeId, search, currentPage, pageSize);
    }

    // 员工处理工单节点
    @PutMapping("/handle")
    @ResponseBody
    @SystemLog(name = "处理工单")
    public R handleWorkOrder(@RequestBody Map<String, String> params) {
        String employeeId = params.get("employeeId");
        String workOrderId = params.get("workOrderId");
        String handleRemark = params.get("handleRemark");
        return employeeService.handleWorkOrder(employeeId, workOrderId, handleRemark);
    }

    // 员工工单巡查数据概览统计
    @GetMapping("/getCount/{employeeId}")
    @ResponseBody
    public R getStat(@PathVariable String employeeId) {
        return employeeService.getStatByEmployee(employeeId);
    }

    // 查询所有员工，按组织和角色分类
    @GetMapping("/getFormedEmployee")
    @ResponseBody
    public R getFormedEmployee() {
        return employeeService.getEmployeeByRole();
    }


}
